package com.cloudptt.api.comm;

import java.io.UnsupportedEncodingException;

public class CheckData 
{
	public static boolean checkOnlyLetterNumber(String testString) {
		String strnumberword = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
		for (int i = 0; i < testString.length(); i++) 
		{
			String temp = testString.substring(i,i+1);
			if(strnumberword.indexOf(temp) < 0 )
			{
				return false;
			}
		}
		return true;
	}

	public static boolean checkErrorWord(String testString) {
		String strnumberword = "\'\"\\%<>=,/";
		for (int i = 0; i < testString.length(); i++) {
			String temp = testString.substring(i, i + 1);
			if (strnumberword.indexOf(temp) > 0 || temp.equalsIgnoreCase("\'")) 
			{
				return false;
			}
		}
		return true;
	}
	public static boolean checkAscii(String str) 
	{
		char[] chararray = str.toCharArray();
		for (int i = 0; i < chararray.length; i++) 
		{
			char testchar = chararray[i];
			if((testchar < 48   
					|| (testchar > 57 && testchar < 65)
					|| (testchar > 90 && testchar < 97)
					||  testchar > 122)) 
			{
				if(testchar != '_' 
						&& testchar != '-')
				{
					return false;
				}
			}
		}
		if(str.length()>63)
		{
			return false;
		}
		return true;
	}

	public static boolean parseBoolean(String strValue)
	{
		try
		{
			return Boolean.parseBoolean(strValue);
		}
		catch(Exception e)
		{
			return false;
		}
	}
	public static int parseInt(String strValue)
	{
		try
		{
			return Integer.parseInt(strValue);
		}
		catch(Exception e)
		{
			return -1;
		}
	}

	public static double parseDouble(String strValue)
	{
		try
		{
			return Double.parseDouble(strValue);
		}
		catch(Exception e)
		{
			return 0;
		}
	}

	public static long parseLong(String strValue)
	{
		try
		{
			return Long.parseLong(strValue);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
	}

	public static float parseFloat(String strValue)
	{
		try
		{
			return Float.parseFloat(strValue);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
	}
	
	public static boolean parseBoolean(int iValue)
	{
		if(iValue == 1)
		{
			return true;
		}
		return false;
	}

	public static int pow2(int b)
	{
		int value = 1;
		value = value << b;
		return value;
	}

	public static boolean checkIP(String ip)
	{
		if(checkStrNull(ip))
		{
			return false;
		}
		if(getCharCount(ip,'.') != 3)
		{
			return false;
		}
		if(!checkIpIntStr(ip))
		{
			return false;
		}
		else
		{
			if(ip.equals("0.0.0.0")
					|| ip.equals("255.255.255.255"))
			{
				return false;
			}
		}
		return true;
	}

	private static int getCharCount(String str,char ch)
	{
		int count = 0;
		for(int i = 0; i< str.length(); i++)
		{
			char indexCh = str.charAt(i);
			if(indexCh == ch)
			{
				count ++;
			}
		}
		return count;
	}
	public static boolean checkMask(String mask) {
		if(checkStrNull(mask))
		{
			return false;
		}
		java.util.StringTokenizer st = new java.util.StringTokenizer(mask, ".");
		// 第一个字节应该大于0x80;
		StringBuffer sb = new StringBuffer();
		String str = st.nextToken();
		// Integer iTmp=Integer.valueOf(str);
		int nTmp = Integer.parseInt(str);
		if (nTmp < 0x80) 
		{
			return false;
		}
		sb.append(Integer.toHexString(nTmp));
		// 将ip地址拼成一个字符串，格式:0xffffffff
		while (st.hasMoreElements()) {
			str = st.nextToken();
			nTmp = Integer.parseInt(str);
			if (nTmp < 0x10) {
				sb.append("0");
			}
			sb.append(Integer.toHexString(nTmp));
		}
		boolean bFirstZero = false;
		long l = Long.parseLong(sb.toString(), 16);
		String strTmp = Long.toBinaryString(l);
		for (int i = 0; i < strTmp.length(); i++) {
			if (!bFirstZero) {
				if ('0' == strTmp.charAt(i)) {
					bFirstZero = true;
				}
			}
			// 如果前面有0，碰到1就是不合法的
			else {
				if ('1' == strTmp.charAt(i)) 
				{
					return false;
				}

			}
		}
		return true;
	}
	public static boolean checkPort(String port)
	{
		if(checkStrNull(port))
		{
			return false;
		}
		if(!checkIntStr(port))
		{
			return false;
		}
		else
		{
			int iPort = Integer.parseInt(port);
			if(iPort < 0 
					|| iPort >65535)
			{
				return false;
			}
		}
		return true;
	}

	public static boolean checkIntStr(String intStr) 
	{
		if(checkStrNull(intStr))
		{
			return false;
		}
		char[] chararray = intStr.toCharArray();
		for (int i = 0; i < chararray.length; i++) 
		{
			int testchar = chararray[i];
			if(testchar > 57 || testchar < 48) 
			{
				return false;
			}
		}
		try
		{
			Integer.parseInt(intStr);
		}
		catch (Exception e) {
			return false;
		}
		return true;
	}
	public static boolean checkIpIntStr(String intStr) 
	{
		String[] str = new String[4];
		//先找到IP地址字符串中.的位置
		int position1 = intStr.indexOf(".");
		int position2 = intStr.indexOf(".", position1 + 1);
		int position3 = intStr.indexOf(".", position2 + 1);
		//将每个.之间的字符串转换成整型
		str[0] = intStr.substring(0, position1);
		str[1] = intStr.substring(position1+1, position2);
		str[2] = intStr.substring(position2+1, position3);
		str[3] = intStr.substring(position3+1);

		for(int i = 0; i < str.length; i++)
		{
			int iValue = 0;
			try {
				iValue = Integer.parseInt(str[i]);
			} catch (NumberFormatException e) 
			{
				return false;
			}
			if (iValue < 0  || iValue >= 254)
			{
				return false;
			}
		}

		char[] chararray = intStr.toCharArray();
		for (int i = 0; i < chararray.length; i++) 
		{
			int testchar = chararray[i];
			if((testchar > 57 || testchar < 48)
					&& testchar != '.') 
			{
				return false;
			}
		}
		int lastindex = intStr.lastIndexOf(".");
		intStr = intStr.substring(lastindex + 1, intStr.length());
		//		int i = 0;
		//		try {
		//			i = Integer.parseInt(intStr);
		//		} catch (NumberFormatException e) {
		//			// e.printStackTrace();
		//			return false;
		//		}
		//		if (i == 0 || i == 255) 
		//		{
		//			return false;
		//		}
		return true;
	}
	public static boolean checkUsername(String userName)
	{
		if(checkStrNull(userName))
		{
			return false;
		}
		else
		{
			if(userName.length()<1 
					|| userName.length()> 32)
			{
				return false;
			}
		}
		return true;
	}

	public static boolean checkPassword(String password)
	{
		if(checkStrNull(password))
		{
			return false;
		}
		else
		{
			if(password.length()<3
					|| password.length()> 32)
			{
				return false;
			}
		}
		return true;
	}

	public static boolean checkStrNull(String str)
	{
		if(str==null 
				|| str.equals(""))
		{
			return true;
		}
		return false;
	}

	public static int getStrLen(String str)
	{
		int len = 0;
		try {
			len = str.getBytes("GBK").length;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return len;
	}
}
